/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.ItemDao;
import com.taobao.snowsnow.hibernate.pojo.Item;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class ItemDaoImpl extends BaseDaoImpl implements ItemDao {
	
	public Item findById(int id) throws Exception {
		return (Item) getHibernateTemplate().get(Item.class, id);
	}
	
	public List<Item> findByName(String name) throws Exception {
		return super.findByProperty(Item.class, "name", name);
	}

	public List<Item> findAll() throws Exception {
		return getHibernateTemplate().find("from Item");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Item.class);
	}

	public List<Item> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Item.class, page, limit);
	}

	@Override
	public Item getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Item as o where o.id = (select min(id) from Item)");
		List<Item> list = query.list();
		if(list.size() !=0)
			return (Item) list.get(0);
		else return null;
	}

	@Override
	public Item getLastObject() throws Exception {
		Query query = getSession().createQuery("from Item as o where o.id = (select max(id) from Item)");
		List<Item> list = query.list();
		if(list.size() !=0)
			return (Item) list.get(0);
		else return null;
	}
}
